import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
  id: root
  visible: true
  width: 640
  height: 480
  title: qsTr("a05")
  Button {
    text: "Ok"
    onClicked: {
      root.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
    }
  }

  Item {
    id: item1
    // focus: true
    y: 50
    Rectangle {
      id: rect1
      width: 200
      height: 200
      color: "#cecece"
    }
    Keys.onPressed: event => {
      rect1.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
    }
    Item {
      id: item2
      focus: true
      y: 250
      Rectangle {
        id: rect2
        width: 100
        height: 100
        color: "#00cece"
      }
      Text {
        id: text2
        text: "text"
        font.pixelSize: 24
      }
      Keys.onPressed: event => {
        rect2.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1);
        // 事件会向上传播, 如果不想传播, 就设置:
        event.accepted = true;
        // 显示按键
        text2.text = event.key;
        // 组合按键 Shift+G
        if ((event.modifiers & Qt.ShiftModifier) && event.key == Qt.Key_G) {
          text2.text = "Shift + G";
          rect2.color = "green";
        } else {
          // 判断键 key
          if (event.key == Qt.Key_W) {
            rect2.color = "red";
          }
        }
      }
    }
  }
}
